package com.zhiying.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.zhiying.domain.OS_Label;
import com.zhiying.domain.OS_Product;
import com.zhiying.page.CmsPageResult;
import com.zhiying.page.CmsResult;
import com.zhiying.page.PageInfo;
import com.zhiying.service.ProductService;
import com.zhiying.until.code.CommonReturnCode;

@Controller
@RequestMapping("/product/list")
public class ProductListController {
	
	
    @Autowired
	private ProductService productService;
    
    @RequiresPermissions("product:info:view")
	@GetMapping(value = "/")
	@ResponseBody
	public Object listProduct(PageInfo pageInfo, @RequestParam(required = false, value = "search") String search) {
    	List<OS_Product> list = productService.SelectAllinfo(pageInfo, search);
		return new CmsPageResult(list, pageInfo.getTotal());
	}
	/**
	 * GET 商品列表页面
	 * @return
	 */
	@RequiresPermissions("product:list:view")
	@RequestMapping("/view")
	public String productList(HttpServletRequest req) {
		return "product/os_product_list";
	}
	

	/**
	 * PUT 上架/下架商品
	 * @return
	 */
	@RequiresPermissions("product:list:audit")
	@PutMapping(value = "/{productId}/audit")
	@ResponseBody
	public Object audit(@PathVariable("productId") Long productId) {
		OS_Product product=new OS_Product();
		product.setProductId(productId);
		int count=productService.UpdateshowInShelve(product);
		return new CmsResult(CommonReturnCode.SUCCESS, count);
		}
	/**
	 * 修改商品信息
	 * */
	@RequiresPermissions("product:list:edit")
	@GetMapping(value = "/{productNumber}/edit")
	  public String Getinfo(HttpServletRequest req,@PathVariable("productNumber") Long productNumber) {
		
		OS_Product productinfo=productService.SelectinfoByproductNumber(productNumber);
		req.setAttribute("productinfo", productinfo);
		return "product/os_product_update";
	}
	/*
	 * 保存编辑信息(新图片)
	 * */
	@RequiresPermissions("product:list:update")
	@ResponseBody
	@PutMapping("/Newupdate")
	public Object NewupdateInfo(OS_Product product,HttpServletRequest req,@RequestParam("file") MultipartFile file) {
			product.setPicImg("images/goods/20170226/"+file.getOriginalFilename());
			File dest = new File("src/main/resources/static/images/goods/20170226/"+file.getOriginalFilename());
		product.setProductNumber(Long.valueOf(req.getParameter("productNumber")));
		product.setName(req.getParameter("name"));
		OS_Label label=new OS_Label();
		label.setLabelId(Integer.valueOf(req.getParameter("labelId")));
		product.setLabel(label);
		product.setShowScore(Integer.valueOf(req.getParameter("showScore")));
		product.setShowPrice(new BigDecimal(req.getParameter("showPrice")));
		product.setIntroduce(req.getParameter("introduce"));
		product.setShowInTop(Integer.valueOf(req.getParameter("showInTop")));
		product.setShowInNav(Integer.valueOf(req.getParameter("showInNav")));
		product.setShowInHot(Integer.valueOf(req.getParameter("showInHot")));
		product.setShowInShelve(Integer.valueOf(req.getParameter("showInShelve")));
		int row=productService.UpdateinfoById(product);
		if(row>0) {
			try {
				FileOutputStream fos=new FileOutputStream(dest);
				fos.write(file.getBytes());
				fos.flush();
				fos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
			return new CmsResult(CommonReturnCode.SUCCESS,row);
		}else {
			return new CmsResult(CommonReturnCode.BAD_REQUEST);			
		}
	}
	/*
	 * 保存编辑信息(旧图片)
	 * */
	@RequiresPermissions("product:list:update")
	@ResponseBody
	@PutMapping("/update")
	public Object updateInfo(OS_Product product) {
		product.setPicImg(null);
		int row=productService.UpdateinfoById(product);
		if(row>0) {
			return new CmsResult(CommonReturnCode.SUCCESS,row);
		}else {
			return new CmsResult(CommonReturnCode.BAD_REQUEST);			
		}
	}
	@RequiresPermissions("product:list:createview")
	@RequestMapping("/createview")
	public String createview() {
		return "product/os_product_create";
	}
	@RequiresPermissions("product:list:create")
	@ResponseBody
	@PutMapping("/create")
	public Object create(@RequestParam("file") MultipartFile file,HttpServletRequest req) {
	    OS_Product product=new OS_Product();
	    product.setPicImg("images/goods/20170226/"+file.getOriginalFilename());
		File dest = new File("src/main/resources/static/images/goods/20170226/"+file.getOriginalFilename());
	    product.setProductNumber(Long.valueOf(req.getParameter("productNumber")));
		product.setName(req.getParameter("name"));
		OS_Label label=new OS_Label();
		label.setLabelId(Integer.valueOf(req.getParameter("labelId")));
		product.setLabel(label);
		product.setShowScore(Integer.valueOf(req.getParameter("showScore")));
		product.setShowPrice(new BigDecimal(req.getParameter("showPrice")));
		product.setIntroduce(req.getParameter("introduce"));
		product.setShowInTop(Integer.valueOf(req.getParameter("showInTop")));
		product.setShowInNav(Integer.valueOf(req.getParameter("showInNav")));
		product.setShowInHot(Integer.valueOf(req.getParameter("showInHot")));
		product.setShowInShelve(Integer.valueOf(req.getParameter("showInShelve")));
		product.setSearchKey(req.getParameter("searchKey"));
		int row=productService.InsertInfo(product);
		if(row>0) {
			try {
				FileOutputStream fos=new FileOutputStream(dest);
				fos.write(file.getBytes());
				fos.flush();
				fos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return new CmsResult(CommonReturnCode.SUCCESS,row);
		}else {
			return new CmsResult(CommonReturnCode.BAD_REQUEST,row);			
		}
	}
	/**
//	 * DELETE 删除商品
//	 * @return
//	 */
	@RequiresPermissions("product:list:delete")
	@DeleteMapping(value = "/{productId}")
	@ResponseBody
	public Object delete(@PathVariable("productId") Long productId) {
		int row=productService.DeletinfoById(productId);
		if(row>0) {
			return new CmsResult(CommonReturnCode.SUCCESS,row);
		}else {
            return new CmsResult(CommonReturnCode.UNAUTHORIZED);			
		}
	}
}
